package com.liu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.entity.User;
import com.liu.service.UserService;
import com.liu.vo.DataView;
import com.liu.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/toUser")
    public String toUser(){
        return "admin/user";
    }

    @RequestMapping("/listUser")
    @ResponseBody
    public DataView listUser(UserVo userVo){
        IPage<User> page = new Page<>(userVo.getPage(), userVo.getLimit());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(userVo.getUsername() != null, "username",userVo.getUsername());
        queryWrapper.like(userVo.getPhone() != null, "phone",userVo.getPhone());
        userService.page(page,queryWrapper);

        DataView dataView = new DataView(page.getTotal(),page.getRecords());
        return dataView;
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public DataView addUser(User user){
        DataView dataView = new DataView();
        boolean b = userService.saveOrUpdate(user);
        if(b){
            dataView.setCode(200);
            dataView.setMsg("新增或者更改用户成功");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("新增或者更改用户失败");
        return dataView;
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public DataView deleteMedicine(Integer mid){
        DataView dataView = new DataView();
        boolean b = userService.removeById(mid);
        if(b){
            dataView.setCode(200);
            dataView.setMsg("用户删除成功");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("用户删除失败");

        return  dataView;
    }

    @RequestMapping("/toUserInfo")
    public String toUserInfo(){
        return "admin/userinfo";
    }

    @RequestMapping("/toRegisters")
    public String toRegister(){
        return "admin/registers";
    }
}
